<<usegrids>>
<table>
<tr><td valign="top">

<table>
<tr><td>
Select group
</td><td>
<select name="group" onchange="groupChanged(this)">
    <option value="0">Select group</option>
    <option value="1">Companies with no homepage/english homepage</option>
    <option value="2">Registered hungarian/foreign companies</option>
    <option value="3">Hungarian/foreign business leaders</option>
    <option value="4">Companies by HS2/4</option>
    <option value="5">Exporter/importer hungarian companies</option>
    <option value="6">Hungarian companies abroad</option>
    <option value="7">Honorary ambassadors</option>
    <option value="8">Hungarian embassies</option>
    <option value="9">Foreign chambers of commerce</option>
    <option value="10">Business partners from hungarian companies</option>
    <option value="11">Not yet updated their profile</option>
    <option value="12">Visitors</option>
</select>
</td><td>
<div id="fp_4" style="display:none;">
<select name="hstype">
    <option value="hs2">HS 2</option>
    <option value="hs4">HS 4</option>
</select>
<input type="text" name="hsvalue">
</div>

<div id="fp_6" style="display:none;">
<select name="countries">
</select>
</div>

</td><td>
<button onclick="startFilter()">Filter</button>
</td></tr>
</table>

<table id="company_email" class="admingrid">
</table>
<div id="company_email_pager"></div>
</tD><td valign="top">

<table class="emailcomposer">
    <tr><td class="label">Email subject</td><td class="input"><input type="text" name="subject"></td></tr>
    <tr><td class="label" colspan=2>Email body<br>
        <textarea class="editor" name="emailtext" style="width:400px; height:330px;">
        </textarea>
    </tD></tr>
</table>
<br>
<label>Send test email to</label><input type="text" name="testemail"><button onclick="sendTestEmail()">Send test email</button><br>

<button id="sendcompanymails" onclick="sendCompanyEmails()">Send email to selected companies</button>
</td></tr>
</table>

<script>
grids.company_email = {
    url : "",
    colNames : [
        "id",
        "Company name",
        "Email address"
    ],
    colModel : [
        { name : "company_id", index : "company_id", hidden : true, key : true},
        { name : "companyname", index : "companyname", width : 270 },
        { name : "email1",        index : "email1",        width : 270 }
    ],
    caption : "Company list",
    multiselect : true,
    multiboxonly : true,
    height : 300
};

createGrids();
createEditors("email_to_groups");

function sendTestEmail() {
    callAdminService("company_email", "sendtestemail", {
        email : jQuery("input[name='testemail']").val(),
        text : jQuery("textarea[name='emailtext']").html(),
        subject : jQuery("input[name='subject']").val()
    }, {
        success : function(result) {
            jQuery("button").attr("disabled", false);
            displayMessage("info", "Mails have been successfully sent");
        }
    });
}

function sendCompanyEmails() {
    var selectedItems = getSelectedRows("company_email");
    jQuery("button").attr("disabled", true);
    callAdminService("company_email", "sendmail", {
        company_ids : selectedItems,
        group : jQuery("select[name='group']").val(),
        emailcount : jQuery("input[name='emailcount']").val(),
        text : jQuery("textarea[name='emailtext']").html(),
        subject : jQuery("input[name='subject']").val()
    }, {
        success : function(result) {
            jQuery("button").attr("disabled", false);
            displayMessage("info", "Mails have been successfully sent");
            var group = jQuery("select[name='group']").val();
            if (group == "13" || group == "14") {
                startFilter();
            }
        },
        failure : function(result, msg) {
            jQuery("button").attr("disabled", false);
            displayMessage("error", msg);
        }
    });
}

function groupChanged(item) {
    jQuery("div[id^='fp_']").hide();
    if (jQuery("div#fp_" + item.value).size() > 0) {
        jQuery("div[id='fp_" + item.value + "']").show("fade", {}, 300);
    }
}

function startFilter() {
    var group = jQuery("select[name='group']").val();
    var country = jQuery("select[name='country']").val();
    var hstype = jQuery("select[name='hstype']").val();
    var hsvalue = jQuery("input[name='hsvalue']").val();
    
    callAdminService("company_email", "filtergroup", {
        group : group,
        country : country,
        hstype : hstype,
        hsvalue : hsvalue
    }, {
        success : function(result) {
            setGridData("company_email", result.griddata);
        }
    });
}

callAdminService("company_email", "getcountries", {}, {
    success : function(result) {
        console.dir(result);
    }
});
</script>
